package io.renren.modules.sys.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.entity.TiananEntity;
import io.renren.modules.sys.service.TiananService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.entity.TiananFuyiEntity;
import io.renren.modules.sys.service.TiananFuyiService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;


/**
 * 提案附议表
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2021-03-17 14:41:00
 */
@RestController
@RequestMapping("sys/tiananfuyi")
public class TiananFuyiController extends AbstractController {
    @Autowired
    private TiananFuyiService tiananFuyiService;
    @Autowired
    private TiananService tiananService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = tiananFuyiService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/lists")
    public R lists(@RequestParam Map<String, Object> params) {
        List<TiananFuyiEntity> list  = tiananFuyiService.list(params);
        return R.ok().put("lists", list);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        TiananFuyiEntity tiananFuyi = tiananFuyiService.getById(id);

        return R.ok().put("tiananFuyi", tiananFuyi);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody TiananFuyiEntity tiananFuyi) {
        tiananFuyi.setUserid(getUserId());
        tiananFuyi.setCreateTime(new Date());
        tiananFuyiService.save(tiananFuyi);
        updateTianan(tiananFuyi);


        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody TiananFuyiEntity tiananFuyi) {
        ValidatorUtils.validateEntity(tiananFuyi);
        tiananFuyi.setUpdateTime(new Date());
        tiananFuyiService.updateById(tiananFuyi);
        updateTianan(tiananFuyi);
        return R.ok();
    }

    /**
     * 更新提案
     * @param tiananFuyi
     */
    private void  updateTianan(TiananFuyiEntity tiananFuyi){
        TiananEntity tiananEntity = new TiananEntity();
        tiananEntity.setId(Integer.parseInt(tiananFuyi.getTianid()));
        tiananEntity.setUpdateTime(new Date());
        tiananEntity.setCreateTime(new Date());
        if (tiananFuyi.getStatus().equals("1")) {
            int size = tiananFuyiService.list(new QueryWrapper<TiananFuyiEntity>()
                    .eq("tianid", tiananFuyi.getTianid())
                    .eq("status", "1")).size();
            int inedx = tiananService.getById(tiananFuyi.getTianid()).getFuyiren().split(",").length;
            if (size == inedx) {
                tiananEntity.setFuyistatus("1");
                tiananEntity.setYushenstatus("0");
            }
        } else {
            tiananEntity.setFuyistatus("2");
        }
        tiananService.updateById(tiananEntity);
    }
    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids) {
        tiananFuyiService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
